package com.android.RssReader.Service;

import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.os.IBinder;

import com.android.RssReader.Main;
import com.android.RssReader.RssReaderApplication;
import com.android.RssReader.Provider.NewsDatabaseHelper;

public class ServiceManager extends Service{

	private static final EventService eventService = new EventService();
	private static final NewsService newsService = new NewsService();
	private static final ScreenService screenService = new ScreenService();
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	private static final NotificationService notificationService = new NotificationService();
	private static boolean started;
	private static Main mainActivity;
	@Override
	public void onCreate() {
		super.onCreate();
	}
	
	public static boolean start() {
		if(ServiceManager.started){
			return true;
		}
		// start Android service
		RssReaderApplication.getInstance().startService(
				new Intent(RssReaderApplication.getInstance(), ServiceManager.class));
		
		
		boolean success = true;

		success &= notificationService.start();
		success &= eventService.start();
		success &= newsService.start();
		success &= screenService.start();

		if(!success){
			RssReaderApplication.LogD(ServiceManager.class, "Failed to start services");
			return false;
		}
		
		if(newsService.getMediaDB().queryCountLocalCategorys() == 0){
			ContentValues contentValues = new ContentValues();
			contentValues.put(NewsDatabaseHelper.COLUMN_NEWS_CAGETORY_NAME, RssReaderApplication.NEWS_CATEGORY_DEFAULT1_NAME);
			contentValues.put(NewsDatabaseHelper.COLUMN_NEWS_CAGETORY_URL, RssReaderApplication.NEWS_CATEGORY_DEFAULT1_URL);
			ServiceManager.getNewsservice().getMediaDB().insertCountLocalCategorys(contentValues);
			contentValues = new ContentValues();
			contentValues.put(NewsDatabaseHelper.COLUMN_NEWS_CAGETORY_NAME, RssReaderApplication.NEWS_CATEGORY_DEFAULT2_NAME);
			contentValues.put(NewsDatabaseHelper.COLUMN_NEWS_CAGETORY_URL, RssReaderApplication.NEWS_CATEGORY_DEFAULT2_URL);
			ServiceManager.getNewsservice().getMediaDB().insertCountLocalCategorys(contentValues);
		}
		
		ServiceManager.started = true;
		return true;
	}
	
	public static boolean stop() {
		if(!ServiceManager.started){
			return true;
		}
		
		// stops Android service
		RssReaderApplication.getInstance().stopService(
				new Intent(RssReaderApplication.getInstance(), ServiceManager.class));
		
		boolean success = true;

		success &= notificationService.stop();
		success &= eventService.stop();
		success &= newsService.stop();
		
		if(!success){
			RssReaderApplication.LogD(ServiceManager.class, "Failed to stop services");
		}
		ServiceManager.started = false;
		return success;
	}

	public static Main getMainActivity() {
		return mainActivity;
	}

	public static void setMainActivity(Main mainActivity) {
		ServiceManager.mainActivity = mainActivity;
	}

	public static NotificationService getNotificationservice() {
		return notificationService;
	}

	public static EventService getEventservice() {
		return eventService;
	}

	public static NewsService getNewsservice() {
		return newsService;
	}

	public static ScreenService getScreenservice() {
		return screenService;
	}
	
	public static boolean isStarted() {
		return started;
	}

	
	public static void exit() {
		stop();
		mainActivity.finish();
		System.exit(0);
	}
	
	
}
